<script setup>
import { RouterView } from 'vue-router'
import { onMounted } from "vue"
import { useRoute } from 'vue-router'

const route = useRoute()
let keepAlive = route.meta.keepAlive

onMounted(() => {
  event()
})

// 沉浸式体验 防退出
function event() {
  document.addEventListener('plusready', function () {
    var webview = plus.webview.currentWebview()
    // 沉浸式体验
    plus.navigator.setStatusBarStyle('light')
    plus.navigator.setStatusBarBackground('#000000')
    // 防退出
    plus.key.addEventListener('backbutton', function () {
      webview.canBack(function (e) {
        if (e.canBack) {
          sessionStorage.setItem('isBack', true)
          webview.back()
        } else {
          var first = null
          plus.key.addEventListener(
            'backbutton',
            function () {
              if (!first) {
                first = new Date().getTime()
                setTimeout(function () {
                  first = null
                }, 1000)
              } else {
                if (new Date().getTime() - first < 1500) {
                  plus.runtime.quit()
                }
              }
            },
            false
          )
        }
      })
    })
  })
}
</script>

<template>
  <keep-alive>
    <RouterView v-if="keepAlive" />
  </keep-alive>
  <RouterView v-if="!keepAlive" />
</template>
